﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Leadtools;
using Leadtools.WinForms;
using SmallERP.FenPian.IView;
using SmallERP.FenPian.Presenter;
using SmallERP.FenPian.Forms;
using SmallERP.MODEL.FenPian;
using System.ServiceModel;
using Smallerp.Contracts;
using Smallerp.Contracts.DataContracts;

namespace SmallERP.FenPian
{
    public partial class FrmFenpian : Form, IFenPianView
    {
       // private FlowLayoutPanel _imgBrowSer = new FlowLayoutPanel();
        private RasterThumbnailBrowser _imgBrowSer=new RasterThumbnailBrowser();
        public FrmFenpian()
        {
            InitializeComponent();
            splitContainer1.Panel2Collapsed = true;

            //_imgBrowSer = new FlowLayoutPanel();
            _imgBrowSer.Width = gridControlPhoto.Width;
            _imgBrowSer.Height = gridControlPhoto.Height;
            _imgBrowSer.Location = gridControlPhoto.Location;
            _imgBrowSer.Anchor = gridControlPhoto.Anchor;
           // _imgBrowSer.AutoScroll = true;
            _imgBrowSer.BorderStyle = BorderStyle.FixedSingle;
            splitContainer1.Panel2.Controls.Add(_imgBrowSer);

            _imgBrowSer.Visible = false;
        }
        private FenPianPresenter _prenster;
        //int SelectModel = 0;
        private void FrmFenpian_Load(object sender, EventArgs e)
        {
            try
            {
                _prenster = new FenPianPresenter(this);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //FrmSelectSubarea frm = new FrmSelectSubarea();
            //frm.ShowDialog();
            SelectModel = Properties.Settings.Default.FenpianModel;
            this.Text = "条码分片,当前连接IP地址:" + DataTransfer.IPadress;
            toolStripComboBoxIsPrinteTicket.SelectedIndex = (Properties.Settings.Default.IsPrintTicket) ? 0 : 1;
        }
        public static FrmFenpian _instance;
        public static FrmFenpian GetInstince
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmFenpian();
                }
                return _instance;

            }
        }
        public bool SetEnterTxtEnabled
        {
            set { throw new NotImplementedException(); }
        }
        public int SelectModel { get; set; }
        public List<UCGrid> CurSelectedGridS
        {
            get { throw new NotImplementedException(); }
        }
        private string _curBarCode = string.Empty;
        public string InpuPhotoCode
        {
            get { return _curBarCode; }
        }

        public void DisPlayGrid(List<UCGrid> UgridS)
        {
            SetLayOutPanel();
            if (tableLayoutPanelGrid.Controls.Count > 0)
            {
                tableLayoutPanelGrid.Controls.Clear();
            }
            foreach (var uc in UgridS)
            {
                
                tableLayoutPanelGrid.Controls.Add(uc);
            }
        }

        public void UpdateGrid(UCGrid grid)
        {
            throw new NotImplementedException();
        }

        public void DisPlayCurGridNameInLbl(string strGridName)
        {
            throw new NotImplementedException();
        }

        public void ClearInputTxtBox()
        {
            throw new NotImplementedException();
        }

        public void DisPlayBaseMsg(string strMsg)
        {
            throw new NotImplementedException();
        }

        public Size GetDisplaySize
        {
            get { return Screen.GetWorkingArea(this.splitContainer1).Size; }
        }

        public bool setClearBtnEnable
        {
            set { throw new NotImplementedException(); }
        }

        public bool setPrintBtnEnable
        {
            set { throw new NotImplementedException(); }
        }

        public bool setSelectedClearBoxEnable
        {
            set { throw new NotImplementedException(); }
        }
        public ChannelFactory<IOperateDBFenPian> _proxyOperateDB { get; set; }//创建代理
        public IOperateDBFenPian _OperateDb { get; set; }

        public void ShowOrderDetail(MyDefineGrid myGrid)
        {
            this.Cursor = Cursors.WaitCursor;
            if (splitContainer1.Panel2Collapsed)
            {
                splitContainer1.Panel2Collapsed = false;
            }
            progressBar1.Visible = false;
            gridControlPhoto.Visible = true;
            _imgBrowSer.Visible = false;
            btnLookImg.Text = "查看缩略图";

            gridControlPhoto.DataSource = myGrid.gridInfo.OrderInfo.PhotoS;
            gridControlPhoto.RefreshDataSource();

            lblOrderProductId.Text = "产品号：" + myGrid.gridInfo.OrderInfo.OrderProductId;
            lblGridName.Text = "格子名：" + myGrid.gridInfo.GridBaseInfo.GridName;
            lblOrderProductName.Text = "产品名：" + myGrid.gridInfo.OrderInfo.OrderName;
            lblPCount.Text = "总数量：" + myGrid.gridInfo.OrderInfo.PhotoS.Count.ToString();
            int status = (int)SmallERP.COM.Status.PhotoStatus.InGrid;
            lblPInCount.Text = "放入数量：" + (myGrid.gridInfo.OrderInfo.PhotoS.Where(p => p.Status == status)).Count().ToString();
            this.Cursor = Cursors.Default;
        }
        delegate void showImagDelegate(List<RasterImageListItem> listImg);
        public void ShowImg(List<RasterImageListItem> listImg)
        {
            if (_imgBrowSer.InvokeRequired)
            {
                _imgBrowSer.Invoke(new showImagDelegate(ShowImg), new object[] { listImg });
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            if (_imgBrowSer.Items.Count > 0)
            {
                _imgBrowSer.Items.Clear();
            }
            
            foreach (var item in listImg)
            {
               // _imgBrowSer.Controls.Add(new RasterImageViewer() { Image = item, SizeMode = Leadtools.RasterPaintSizeMode.Stretch });
                 _imgBrowSer.Items.Add(item);

            }
            this.Cursor = Cursors.Default;
        }

        public void SetProcessGo()
        {
            if (!progressBar1.Visible)
            {
                progressBar1.Visible = true;
            }

            if (progressBar1.Value >= progressBar1.Maximum)
            {
                progressBar1.Value = 0;
            }
            progressBar1.Value++;
        }

        public void SetProcessOver()
        {
            progressBar1.Value = 0;
            progressBar1.Visible = false;
        }

        const int GRIDNAMECOUNT = 4;//最多显示4个格子
        int curGridCount = 0;
        StringBuilder strGridNameList = new StringBuilder();
        delegate void ShowGridNameDelegate(string gridName);
        public void ShowGridName(string gridName)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ShowGridNameDelegate(ShowGridName), new string[] { gridName });
                return;
            }
            curGridCount++;
            if (curGridCount <= GRIDNAMECOUNT)
            {
                strGridNameList.Append(gridName);
            }
            else
            {
                int index = strGridNameList.ToString().IndexOf('A', 1);
                if (index > 0)
                {
                    strGridNameList.Remove(0, index);
                    strGridNameList.Append(gridName);
                    curGridCount--;
                }
            }

            lblShowGridName.Text = strGridNameList.ToString();
        }


        public void CloseDetailPanel()
        {
            if (!splitContainer1.Panel2Collapsed)
            {
                splitContainer1.Panel2Collapsed = true;
            }
        }

        private void SetLayOutPanel()
        {
            tableLayoutPanelGrid.ColumnStyles.Clear();
            tableLayoutPanelGrid.RowStyles.Clear();
            tableLayoutPanelGrid.Padding = new Padding(0);
            tableLayoutPanelGrid.RowCount = DataTransfer.CurSubArea.WidthNum;
            tableLayoutPanelGrid.ColumnCount = DataTransfer.CurSubArea.HeightNum;
        }
        //EAN.ClassInterface ecif = new EAN.ClassInterface();
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            toolStripButton5.Enabled = false;
            _curBarCode = txtBoxPhotoBarCode.Text.Trim();
            //_curBarCode = ecif.DeEAN13(_curBarCode);
            txtBoxPhotoBarCode.Clear();
            _prenster.AssionGrid();
            toolStripButton5.Enabled = true;
        }

        private void txtBoxPhotoBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar == (int)Keys.Enter)
            //{
            //    toolStripButton5.Enabled = false;
            //    _curBarCode = txtBoxPhotoBarCode.Text.Trim();
            //    txtBoxPhotoBarCode.Clear();
            //    _prenster.AssionGrid();
            //    toolStripButton5.Enabled = true;
            //}
        }
        private StringBuilder strBPhotoBarCode = new StringBuilder();
        private void FrmFenpian_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!txtBoxPhotoBarCode.Focused)
            {
                if (char.IsDigit(e.KeyChar))
                {
                    if (strBPhotoBarCode.Length >= 13)
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        strBPhotoBarCode.Append(e.KeyChar);
                    }

                    txtBoxPhotoBarCode.Text = strBPhotoBarCode.ToString();
                }
            }


            if (e.KeyChar == (int)Keys.Enter)
            {
                if (txtBoxPhotoBarCode.Text.Length > 0)
                {
                    toolStripButton5.PerformClick();
                    strBPhotoBarCode = new StringBuilder();
                }
            }
            else if (e.KeyChar == (int)Keys.Escape)
            {
                if (!splitContainer1.Panel2Collapsed)
                {
                    splitContainer1.Panel2Collapsed = true;
                }
            }
        }

        private void btnLookImg_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            //加载缩略图
            if (btnLookImg.Text == "查看缩略图")
            {
                btnLookImg.Enabled = false;
                _imgBrowSer.Visible = true;
                gridControlPhoto.Visible = false;

                _prenster.GetImageList();
                btnLookImg.Text = "查看列表";
                btnLookImg.Enabled = true;
            }
            else if (btnLookImg.Text == "查看列表")
            {
                btnLookImg.Enabled = false;
                _imgBrowSer.Controls.Clear();
                _imgBrowSer.Visible = false;
                gridControlPhoto.Visible = true;
                btnLookImg.Text = "查看缩略图";
                btnLookImg.Enabled = true;
            }
            this.Cursor = Cursors.Default;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //强制打印选中的格子
            this.Cursor = Cursors.WaitCursor;
            _prenster.ForcePrint();
            this.Cursor = Cursors.Default;
        }

        private void btnClearBox_Click(object sender, EventArgs e)
        {
            //清空选中的格子
            this.Cursor = Cursors.WaitCursor;
            _prenster.ClearSelectedGrid();
            this.Cursor = Cursors.Default;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //复检功能
            FrmReCheck frmCheck = FrmReCheck.GetInstance;
            frmCheck.OperateDb = _OperateDb;
            frmCheck.ShowDialog();
        }

        private void tsbAdvance_Click(object sender, EventArgs e)
        {
            //条件清柜
            _prenster.ConditionPriorityGrid();
        }

        private void tsbAdvanceProduct_Click(object sender, EventArgs e)
        {
            //清柜
            _prenster.PrioritySelectedGird();
        }

        private void tsbtnLogout_Click(object sender, EventArgs e)
        {
            //刷新格子
            this.Cursor = Cursors.WaitCursor;
            tsbtnLogout.Enabled = false;
            _prenster.RefreshData();
            this.Cursor = Cursors.Default;
            tsbtnLogout.Enabled = true;
        }

        private void gridViewphoto_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.Name == "Status")
                {
                    switch (((PhotoDCT)gridViewphoto.GetRow(e.RowHandle)).Status)
                    {
                        case (int)COM.Status.PhotoStatus.Printed: e.DisplayText = "打印完成";
                            break;
                        case (int)COM.Status.PhotoStatus.WaitPB: e.DisplayText = "等待拼版";
                            break;
                        case (int)COM.Status.PhotoStatus.WaitProductBigPic: e.DisplayText = "等待生成大图";
                            break;

                        case (int)COM.Status.PhotoStatus.InGrid: { e.DisplayText = "格子中";
                           }
                            break;
                        case (int)COM.Status.PhotoStatus.Producted:
                            {
                                e.DisplayText = "生成大图完成";
                            }
                            break;
                        default:
                            {
                                e.DisplayText = "";
                            }
                            break;
                    }
                }
              
            }
        }

        private void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButtonChange_Click(object sender, EventArgs e)
        {
            _prenster.Clear();
            _prenster.ChangeAreas();

        }

        private void gridViewphoto_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >=0)
            {
                PhotoDCT view = (PhotoDCT)gridViewphoto.GetRow(e.RowHandle);
                if (view.Status == (int)COM.Status.PhotoStatus.InGrid)
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
            }
        }

        private void toolStripButtonReturn_Click(object sender, EventArgs e)
        {
            FrmReturn frm =  FrmReturn.GetInstance;
            frm.OperateDb = _OperateDb;
            frm.Show();
        }

        private void FrmFenpian_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认退出分片？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) 
            {
                e.Cancel = false;
            }
        else
            {
                e.Cancel = true;
            }
           
        }

        private void toolStripComboBoxModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (toolStripComboBoxModel.SelectedIndex == 0)
            {
                Properties.Settings.Default.FenpianModel = 0;

            }
            else
            {
                Properties.Settings.Default.FenpianModel = 1;

            }
            Properties.Settings.Default.Save();
        }

        private void checkBoxIsPrintDialog_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.IsShowPrintDialog = checkBoxIsPrintDialog.Checked;
            Properties.Settings.Default.Save();
        }

        private void toolStripComboBoxIsPrinteTicket_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (toolStripComboBoxIsPrinteTicket.SelectedIndex == 1)
            {
                Properties.Settings.Default.IsPrintTicket = false;
            }
            else
            {
                Properties.Settings.Default.IsPrintTicket = true;
 
            }
            Properties.Settings.Default.Save();
        }
    }
}
